on 1:MP3END: {
  if %mp3con == 1 {
    /mp3
  }
}


;;;;;;;;;;;;;;;;;;;;Main;;;;;;;;;;;;;;;;;
dialog mp3 {
  title " x-L0st mp3 Player"
  size -1 -1 200 140
  option dbu
  button "&Close",1, 140 108 30 10,ok
  text "Total mp3's:",2, 88 90 59 13
  edit "",3, 120 90 20 10, read, autohs, right
  edit "",5, 20 25 160 10, read, autohs, center
  text "Mb",6, 32 48 20 10
  edit "",7,  11 46 19 10, read, autohs, right
  text "Mins",8, 32 62 50 10  
  text "Current Song:",300, 22 17 50 8
  edit "",9, 11 60 19 10, read, autohs, right
  text "Kbps",10, 32 76 50 8
  edit "",11, 11 74 19 10, read, autohs, right
  text "Khz",12, 32 90 50 8
  edit "",13, 11 88 19 10, read, autohs, right
  edit "",15, 11 102 35 10, read, autohs, right
  button "Play",21,50 40 40 10
  button "Directory",22,50 60 60 10
  button "Stop",25,95 40 40 10
  button "Mp3 List",27,120 60 60 10
  text " -=[x-L0st]=-",28, 94 74 23 10, center
  check "Random",31,123 2 65 10, push
  check "Show Chan",32,6 2 65 10, push
  link "www.xAciD-Outlawsx.cjb.net ",33, 60 118 130 20
  check "Continuous",35,73 2 48 10, push
  check "Pause",36,140 40 40 10, push
  button "Down",37,70 73 20 10
  button "Up",38,124 73 20 10
  ;noborder


}

on 1:dialog:mp3:init:0: {
  set %mp3d 1
  orale
  did -ra mp3 28 Vol $round($calc(( $vol(mp3) / 650 ) - 1 ),0) $+ %
  if ( %mp3ran == 1 ) did -c mp3 31
  if ( %mp3con == 1 ) did -c mp3 35
  if ( %mp3pau == 1 ) did -c mp3 36
  if ( %mp3r1 == 1 ) did -c mp3 32
}



on 1:dialog:mp3:sclick:*: {
  if ($did == 1) { unset %mp3d }
  if ($did == 21) { /mp3 }
  if ($did == 34) { /mp32 }
  if ($did == 22) { .timer -m 1 10 /dialog -m mp3dir mp3dir }
  if ($did == 23) { .timer -m 1 10 /dialog -m mp31 mp31 }
  if ($did == 24) { .timer -m 1 10 /mp }
  if ($did == 25) { 
    splay -wmp stop
    set %mp3pau 0
    if %mp3d == 1 {
      did -u mp3 36
    }
  }
  if ($did == 27) { .timer -m 1 0 /dialog -m mp32 mp32 }
  if ($did == 29) { mp3bajar 1 }
  if ($did == 30) { mp3subir 1 }
  if ($did == 37) { mp3bajar 2 }
  if ($did == 38) { mp3subir 2 }
  if ($did == 31) {
    if %mp3ran == 1 { set %mp3ran 0 }
    else { set %mp3ran 1 }
  }
  if ($did == 32) {
    if %mp3r1 == 1 { set %mp3r1 0 }
    else { set %mp3r1 1 }
  }
  if ($did == 35) {
    if %mp3con == 1 { set %mp3con 0 }
    else { set %mp3con 1 }
  }
  if ($did == 36) {
    if %mp3pau == 0 { set %mp3pau 1 | splay pause }
    else { set %mp3pau 0 | splay resume }
  }
}




dialog mp3dir {
  option dbu
  title "Select A Directory"
  size -1 -1 155 70
  button "Close",1, 3 50 25 10,ok
  button "Add",2, 3 10 25 10
  button "Erase",3, 3 30 25 10
  list 5,33 8 120 60, sort
}


on 1:dialog:mp3dir:init:0: {
  mp3dir2
  if ( $did(mp3dir,5).lines == 0 ) .timer -m 1 10 mp3dir
  if ( $did(mp3dir,5,0).sel == 0 ) did -b mp3dir 3
}

on 1:dialog:mp3dir:sclick:*: {
  if $did == 1 {
    if ( $did(mp3dir,5).lines == 0 ) .timer -m 1 10 mp3dir
    mp3dir1
    if %mp3d == 1 {
      orale
      dialogmp3
    }
  }
  if $did == 2 {
    mp3dira
  }
  if $did == 3 {
    did -d mp3dir 5 $did(mp3dir,5,1).sel
    did -b mp3dir 3
    if ( $did(mp3dir,5).lines == 0 ) .timer -m 1 10 mp3dir
  }
  if $did == 5 {
    did -e mp3dir 3
  }
}


dialog MP32 {
  title "mp3 List"
  size -1 -1 175 225
  option dbu
  button "Close",2,75 212 26 10, cancel
  ;  edit "",5,3 10 120 10, center,autohs, hide
  LIST 7,3 13 170 203,hsbar, vsbar
}


on 1:dialog:mp32:init:0: {
  did -r mp32 7
  :loopd
  if ( ** isin %mp3searchkey ) { set %mp3searchkey $replace(%mp3searchkey,**,*) | goto loopd }
  setmp32
  if ( $exists(mp3search.txt) ) .remove mp3search.txt
  filter -wf @mp32 mp3search.txt 
  set %mp3i 1
  set %mp3j $lines(mp3search.txt)
  :loop
  if ( %mp3j >= %mp3i ) {
    set %mp3searchr $read -nl $+ %mp3i mp3search.txt
    set %mp3searchr1 $nopath(%mp3searchr)
    set %mp3searchr1 $replace(%mp3searchr1,$chr(95),$chr(32))
    did -a mp32 7 %mp3searchr1
    inc %mp3i
    goto loop    
  }
  unset %mp3i
  unset %mp3j
  unset %mp3searchkey
  unset %mp3searchr 
  unset %mp3searchr1
  unset %mp3searchfile
}


on 1:dialog:mp32:dclick:7: {
  set %mp3abc $read -nl $+ $did(7).sel $nopath(mp3search.txt)
  set %mp3a %mp3abc
  splay %mp3a

  if ($fline(@mp32,%mp3a,1) != $null ) {
    set %mp3num $fline(@mp32,%mp3a,1)
  }

  ;  unset %mp3num
  unset %mp3abc
  if %mp3d == 1 orale
  if %mp3r1 == 1 /mp3r1
  dialog -c mp32
  .remove mp3search.txt
  /dialog -v mp3
}

on 1:DIALOG:mp32:sclick:2: {
  .remove mp3search.txt
  /dialog -v mp3
  unset %mp3searchr1
}

;;;;;;;;Menu;;;;;;;;
menu menubar,channel,status {
  x-L0st mp3 
  .Play:/dialogmp3
  .Fkeys:/echo -a F1 = Random F2 = Mp3 Player F3 = Stop All
}

;;;;;;;;;;;;aliases;;;;;;;;;;;;;

alias -l setmp32 {
  if ( %mp3dir == $null ) /dialog -m mp3dir mp3dir
  /clear -l @mp32
  /window -elhns @mp32
  /clear -l @mp32
  set %mp3player.tmp 0
  while (%mp3player.tmp < $gettok(%mp3dir,0,32)) {
    if $findfile($gettok(%mp3dir,$calc(%mp3player.tmp + 1),32),*.mp3,0,@mp32) !isnum {
      aline @mp32 $findfile($gettok(%mp3dir,$calc(%mp3player.tmp + 1),32),*.mp3,0,@mp32)
    }
    inc %mp3player.tmp
  }
  unset %mp3player.tmp

}

alias -l dialogmp3 {
  if ( $dialog(mp3) ) {
    dialog -o mp3
    dialog -n mp3
  }
  else dialog -md mp3 mp3
}


alias -l mp {
  set %mp3a $sfile($mp3dir Sounds,Take a file,Play!)
  if %mp3a != $null {
    /splay %mp3a
    if %mp3d == 1 {
      orale
    }
  }
  dialog -v mp3

}

alias -l mp3 {
  if %mp3ran == 1 {
    if ( %mp3dir == $null ) /dialog -m mp3dir mp3dir
    set %mp3num.1 $rand(1,$matchtok(%mp3dir,:,0,32))
    set %mp3dirtempo $gettok(%mp3dir,%mp3num.1,32)
    set %mp3num $rand(1,$findfile(%mp3dirtempo,*.mp3,0))
    set %mp3abc $findfile(%mp3dirtempo,*.mp3,%mp3num)
    set %mp3a %mp3abc
    /splay %mp3a
    ;$shortfn(%mp3abc)
    unset %mp3abc
    unset %mp3dirtempo
    unset %mp3num.1
    if %mp3d == 1 orale
    if %mp3r1 == 1 /mp3r1
  }

  if %mp3ran == 0 {
    if ( %mp3dir == $null ) /dialog -m mp3dir mp3dir
    if %mp3num == %mp3count1 { /set %mp3num 0 }
    inc %mp3num
    setmp32
    set %mp3a $line(@mp32,%mp3num)
    /splay %mp3a
    if %mp3d == 1 orale
    if %mp3r1 == 1 /mp3r1

  }
}

alias -l mp32 {
  mp3count
  if ( %mp3dir == $null ) /dialog -m mp3dir mp3dir
  dec %mp3num
  if %mp3num == 0 { 
    set %mp3num %mp3count1
  }
  setmp32
  set %mp3a $line(@mp32,%mp3num)
  /splay %mp3a
  if %mp3d == 1 orale
  if %mp3r1 == 1 /mp3r1
}

alias -l mp3alength {
  set %mp3alength $mp3(%mp3a).length
  set %mp3ablength $int($calc(%mp3alength / 1000))
  set %mp3blength $int($calc(%mp3alength / 60000))
  set %mp3clength $calc(%mp3ablength - (%mp3blength * 60))
  if %mp3clength < 10 {
    set %mp3clength $chr(48) $+ %mp3clength
  }
}

alias -l orale {
  mp3count
  /mp3alength 
  did -ra mp3 5 $replace($nopath(%mp3a),$chr(95),$chr(32))
  did -ra mp3 7  $round($calc(($file(%mp3a).size)/1048576),1)
  did -ra mp3 9  %mp3blength $+ : $+ %mp3clength
  did -ra mp3 11 $mp3(%mp3a).bitrate
  did -ra mp3 13 $calc(($mp3(%mp3a).sample)/1000)
  did -ra mp3 15 $mp3(%mp3abc).bitrate $mp3(%mp3a).mode 
  did -ra mp3 3 %mp3count1
  set %mp3pau 0
  did -u mp3 36
}

alias -l mp3count {
  unset %mp3count 
  unset %mp3count1
  set %mp3player.tmp 0
  while (%mp3player.tmp < $gettok(%mp3dir,0,32)) {
    set %mp3count $addtok(%mp3count,$findfile($gettok(%mp3dir,$calc(%mp3player.tmp + 1),32),*.mp3,0),32)
    inc %mp3player.tmp
  }
  unset %mp3player.tmp
  set %mp3player.tmp 0
  while (%mp3player.tmp < $gettok(%mp3dir,0,32)) {
    set %mp3count1 $calc(%mp3count1 + $gettok(%mp3count,$calc(%mp3player.tmp + 1),32))
    inc %mp3player.tmp
  }
  unset %mp3player.tmp
}

alias -l mp3search {
  did -r mp31 40
  did -r mp31 49
  did -a mp31 49 Searching... Please wait.
  set %mp3searchkey $did(mp31,45)
  set %mp3searchkey $replace(%mp3searchkey,$chr(32),$chr(42))
  set %mp3searchkey $replace(%mp3searchkey,$chr(95),$chr(42))
  set %mp3searchkey $chr(42) $+ %mp3searchkey $+ $chr(42)
  :loopd
  if ( ** isin %mp3searchkey ) { set %mp3searchkey $replace(%mp3searchkey,**,*) | goto loopd }
  /setmp32
  if ( $exists(mp3search.txt) ) .remove mp3search.txt
  filter -wf @mp32 mp3search.txt %mp3searchkey
  set %mp3i 1
  set %mp3j $lines(mp3search.txt)
  :loop
  if ( %mp3j >= %mp3i ) {
    set %mp3searchr $read -nl $+ %mp3i mp3search.txt
    set %mp3searchr1 $nopath(%mp3searchr)
    did -a mp31 40 $replace(%mp3searchr1,$chr(95),$chr(32))
    did -o mp31 49 1 Loading results ( %mp3i of $filtered )... Please wait
    inc %mp3i
    goto loop
  }
  did -r mp31 49
  did -a mp31 49 Results: ( $+ $filtered found)
  unset %mp3i
  unset %mp3j
  unset %mp3searchkey
  unset %mp3searchr 
  unset %mp3searchfile
}

alias -l mp3bajar { /set %mp3vol $calc($vol(mp3) - ( $1 * 5000 )) | /vol -p %mp3vol | /unset %mp3vol | did -ra mp3 28 Vol $round($calc( $vol(mp3) / 650 ),0) $+ % }

alias -l mp3subir { /set %mp3vol $calc($vol(mp3) + ( $1 * 5000 )) | /vol -p %mp3vol | /unset %mp3vol | did -ra mp3 28 Vol $round($calc(($vol(mp3) / 650 ) - 1 ),0) $+ % }

alias -l mp3r1 /amsg 4(15Playing4)14-0=140[14 $+ $replace($nopath(%mp3a),$chr(95),$chr(32)) $+ 0]14-0-14-0[14 $+ $round($calc(($file(%mp3a).size)/1048576),1) $+ mb/ $+ $int($calc(%mp3alength / 60000)) $+ : $+ $calc(%mp3ablength - (%mp3blength * 60)) $+ mins 0]0x-L0st4(14mp11 34) 
}
alias -l mp3dira {
/set %mp3dir $shortfn($sdir(c:,"directory"))
mp3dir
}
alias -l mp3dir {
if %mp3dir == $null {
  /set %mp3dir $shortfn($sdir(c:,"directory"))
}
mp3dir2
if ( $did(mp3dir,5,1) == $null ) did -d mp3dir 5 1
dialog -v mp3dir
}


alias -l mp3dir1 {
unset %mp3dir
set %mp3player.tmp 0
while (%mp3player.tmp < $did(mp3dir,5).lines) {
  set %mp3dir %mp3dir $did(mp3dir,5,$calc(%mp3player.tmp + 1))
  inc %mp3player.tmp
}
unset %mp3player.tmp
}
alias -l mp3dir2 {
set %mp3player.tmp 0
while (%mp3player.tmp < $gettok(%mp3dir,0,32)) {
  did -a mp3dir 5 $gettok(%mp3dir,$calc(%mp3player.tmp + 1),32)
  inc %mp3player.tmp
}
unset %mp3player.tmp
}


;;;;;;;;;;;;F1 key;;;;;;;;;;;;;
alias F1 mp3

;;;;;;;;;;;;F2 key;;;;;;;;;;;;;
alias F2 dialogmp3

;;;;;;;;;;;;F3 key;;;;;;;;;;;;;
alias F3 { 
/splay -wmp stop
set %mp3pau 0
if %mp3d == 1 {
  did -u mp3 36
}
}
